package org.groupg.practice;

import com.univocity.parsers.annotations.Parsed;
import com.univocity.parsers.common.processor.BeanListProcessor;
import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
import java.io.File;
import java.util.List;

public class BeanReaderExample {

    // 定义数据模型
    public static class Person {
        @Parsed(field = "id")
        private int id;

        @Parsed(field = "name")
        private String name;

        @Parsed(field = "email")
        private String email;

        @Override
        public String toString() {
            return "Person{id=" + id + ", name='" + name + "', email='" + email + "'}";
        }
    }

    public static void main(String[] args) {
        File csvFile = new File("data/people.csv");

        // 配置Bean处理器
        BeanListProcessor<Person> rowProcessor = new BeanListProcessor<>(Person.class);

        CsvParserSettings settings = new CsvParserSettings();
        settings.setProcessor(rowProcessor);
        settings.setHeaderExtractionEnabled(true);

        CsvParser parser = new CsvParser(settings);
        parser.parse(csvFile);

        // 获取所有解析的Bean
        List<Person> persons = rowProcessor.getBeans();

        // 输出结果
        for (Person person : persons) {
            System.out.println(person);
        }
    }
}
